package com.java.IO.buffered_stream;

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class BufferedOutputStreamAPI {
    public static void main(String[] args) throws IOException {
        String path = "File和IO流\\com\\java\\files\\byte_stream.txt";
        
        // 创建对象：创建规则主要依赖于构造形参OutputStream的子类
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(path));
        
        // write：写出数据到缓冲区，若参数为整数则会转为ASCII对应的字符。注意此时写出目标地内是无写出数据的
        bufferedOutputStream.write(97); // a
        bufferedOutputStream.write('9'); // 9
        bufferedOutputStream.write('中'); // -
        bufferedOutputStream.write("\r\n".getBytes()); // 换行(Windows\r\n、Linux\n、Mac\r)
        bufferedOutputStream.write(new byte[]{97, 98, 99, 100}); // abcd
        bufferedOutputStream.write(new byte[]{97, 98, 99, 100, 101, 102, 103}, 3, 2); // de
        
        // flush：缓冲区数据写出到目标地并清空缓冲区。打断点到此并观察断点执行前后目标地数据
        bufferedOutputStream.flush();
        
        // close：关闭流并释放资源，若此前未执行flush则充当部分flush作用将缓冲区数据写出到目标地
        bufferedOutputStream.close();
        bufferedOutputStream.close();
        bufferedOutputStream.close();
    }
}
